package priorityQueue;

import java.util.Comparator;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-09-19
 * Time: 8:20
 */
class Card implements Comparable<Card> {
    //花色 + 数字
    public String suit;//花色
    public int rank;//数字范围（1~4）

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "Card{" +
                "suit='" + suit + '\'' +
                ", rank=" + rank +
                '}';
    }

    //重写了equals方法：
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Card card = (Card) o;
        return rank == card.rank && Objects.equals(suit, card.suit);
    }

    @Override
    public int hashCode() {
        return Objects.hash(suit, rank);
    }

    //重新实现Comparable接口
    @Override
    public int compareTo(Card o) {
        return this.rank - o.rank;
    }
}
class SuitComparator implements Comparator<Card> {
    @Override
    public int compare(Card o1, Card o2) {
        //return o1.rank - o2.rank;
        return o1.suit.compareTo(o2.suit);
    }
}
public class Test {
    public static void main(String[] args) {
        Card card1 = new Card("♥", 10);
        Card card2 = new Card("♥", 20);
        //第三种方法：重新实现Comparator接口，这个接口比较灵活，可以自己定义哪个属性比较大小，一般在外部自己实现
        SuitComparator suitComparator = new SuitComparator();
        if(suitComparator.compare(card1, card2) > 0) {
            System.out.println("card1 == card2");
        } else {
            System.out.println("card1 != card2");
        }
    }
    public static void main2(String[] args) {
        Card card1 = new Card("♥", 10);
        Card card2 = new Card("♥", 20);
        //第二种方法：用Comparable接口来实现比较，这个一般用在内部类，（不灵活，不能随时改变）
        //也需要在Card类中重新实现（重写）
        if(card1.compareTo(card2) > 0) {
            System.out.println("card1 == card2");
        } else {
            System.out.println("card1 != card2");
        }
    }
    public static void main1(String[] args) {
        Card card1 = new Card("♥", 10);
        Card card2 = new Card("♥", 20);
        //第一种方法：用equals方法来比较：只能比较是否相等 不能比较大小
        if(card1.equals(card2)) {
            System.out.println("card1 == card2");
        } else {
            System.out.println("card1 != card2");
        }
    }
}
